var element = layui.element,
    layer = layui.layer,
    table = layui.table ,
    $ = layui.jquery
,tablecols1 = [
    {checkbox: true, width:50, fixed: true}
    ,{field:'arrangeName', title:'安排名称', fixed: true}
    ,{field:'remindTime', title:'提醒时间', fixed: true}
    ,{field:'executionLevel', title:'执行级别', fixed: true}
    ,{field:'content', title:'内容', fixed: true}
    ,{title: "操作", templet: "#messageeditor",width:248}
],tablecols2=[
    {checkbox: true, width:50, fixed: true}
    ,{field:'theme', title:'主题', fixed: true}
    ,{field:'content', title:'内容', fixed: true}
    ,{field:'publisherName', title:'发布人', fixed: true}
    ,{field:'createTime', title:'创建时间', fixed: true}
    ,{title: "操作", templet: "#messageeditor",width:248}
],tablecols3 =[
    {checkbox: true, width:50, fixed: true}
    // ,{field:'customerName', title:'客户名称', fixed: true}
    ,{field:'theme', title:'主题', fixed: true}
    ,{field:'contact', title:'联系人', fixed: true}
    ,{field:'remindTime', title:'提醒时间', fixed: true}
    ,{field:'content', title:'内容', fixed: true}
    ,{title: "操作", templet: "#messageeditor",width:248}
] //表头
,messagetype="1";//默认消息类型


element.on('nav(messagenav)', function(elem){
    if(elem.text()=="待办事项增加")messagetype="1",uptable(1);
    else if(elem.text()=="公告栏增加")messagetype="2",uptable(2);
    else if(elem.text()=="个人备忘录增加")messagetype="3",uptable(3);
    return false;
});
//初始化表格
table.render({
    elem: '#message'
    ,type:"get"
    ,url:"message/selectByPage"
    ,where:{
        "type":messagetype,
    }
    ,toolbar: '#toolbarDemo'
    ,cols: [tablecols1]
    ,id: 'messagetableid'
    ,height: $(document).height() - $('#message').offset().top - 50
    ,page: true
    ,limit: 100
    ,limits:[100, 100, 200, 300, 500, 1000]
});
//工具栏
table.on('toolbar(messagetable)', function(obj){
    var checkStatus = table.checkStatus(obj.config.id);
    switch(obj.event){
        case 'del':
            var data = checkStatus.data;
            if(data.length==0){
                layer.msg("请选择要删除的数据");
                return false;
            }
            var delid = [];
            for (var i = 0;i<data.length;i++){
                delid.push(data[i].id)
            }
            if(delmessage(delid)){

                layer.msg("删除成功");
                refrash();
            }else{
                layer.msg("删除失败");
            }
            //layer.msg('需要删除的数据ID为：'+ JSON.stringify(delid) + ',执行删除');
            //table.reload(); //删除成功之后刷新表格
            break;
    };
});
//监听工具条
table.on('tool(messagetable)', function(obj){
    var data = obj.data;
    if(obj.event === 'del'){
        layer.confirm('真的删除行么', function(index){
            //data.id
            var delid=[];
            delid.push(data.id);
            if(delmessage(delid)){

                layer.msg("删除成功");
                refrash();
                obj.del();
                layer.close(index);
            }else{
                layer.msg("删除失败");
            }

        });
    } else if(obj.event === 'edit'){
        if(messagetype == "1"){
            parent.intab("views/jsp/BasicData/tpl/newuparrangement.jsp?id="+data.id+"&type=1","修改待办事项");
        }else if(messagetype == "2"){
            parent.intab("views/jsp/BasicData/tpl/newupannouncement.jsp?id="+data.id+"&type=2","修改公告栏");
        }else if(messagetype == "3"){
            parent.intab("views/jsp/BasicData/tpl/newupcustomercontact.jsp?id="+data.id+"&type=3","修改个人备忘录");
        }
    }

});

//触发事件
var active = {
    arrangement: function(){
        messagetype="1";
        parent.intab("views/jsp/BasicData/tpl/newuparrangement.jsp?type="+messagetype,"新增待办事项");
    }
    ,announcement: function(othis){
        messagetype="2";
        parent.intab("views/jsp/BasicData/tpl/newupannouncement.jsp?type="+messagetype,"新增公告栏");
    }
    ,customercontact: function(othis){
        messagetype="3";
        parent.intab("views/jsp/BasicData/tpl/newupcustomercontact.jsp?type="+messagetype,"新增个人备忘录");
    }
};

$('#messagenav .layui-nav-item a .layui-btn').on('click', function(){
    var othis = $(this), messagetype = othis.data('messagetype');
    active[messagetype] ? active[messagetype].call(this, othis) : '';
    event.stopPropagation();
});
//分页查询
function uptable(types) {
    messagetype=types;
    $("#myTable").empty();
    $("#myTable").append('<table class="layui-table" id="message" lay-filter="messagetable"></table>');
    if(types=="1"){//待办事项增加
        table.render({
            elem: '#message'
            ,type:"get"
            ,url:"message/selectByPage"
            ,where:{
                "type":types,
            }
            ,toolbar: '#toolbarDemo'
            ,cols: [tablecols1]
            ,id: 'messagetableid'
            ,height: $(document).height() - $('#message').offset().top - 50
            ,page: true
            ,limit: 100
            ,limits:[100, 100, 200, 300, 500, 1000]
        });
    }else if(types=="2"){//公告栏增加
        table.render({
            elem: '#message'
            ,type:"get"
            ,url:"message/selectByPage"
            ,where:{
                "type":types,
            }
            ,toolbar: '#toolbarDemo'
            ,cols: [tablecols2]
            ,id: 'messagetableid'
            ,height: $(document).height() - $('#message').offset().top - 50
            ,page: true
            ,limit: 100
            ,limits:[100, 100, 200, 300, 500, 1000]
        });
    }else{
        table.render({//个人备忘录增加
            elem: '#message'
            ,type:"get"
            ,url:"message/selectByPage"
            ,where:{
                "type":types,
            }
            ,toolbar: '#toolbarDemo'
            ,cols: [tablecols3]
            ,id: 'messagetableid'
            ,height: $(document).height() - $('#message').offset().top - 50
            ,page: true
            ,limit: 100
            ,limits:[100, 100, 200, 300, 500, 1000]
        });
    }

}

/**
 * 批量删除
 * @param ids
 * @returns {boolean}
 */
function  delmessage(ids) {
    var tf=false;
    $.ajax({
        type : 'post',
        data:{"ids":JSON.stringify(ids)},
        url:'message/delmessage',
        dataType:"json",
        async:false,
        success : function (da) {
            if(da.status==200 ){
                tf=true;
            }
        }
    });
    return tf;
}

function refrash() {
    table.reload('messagetableid', {
        type:"get"
        ,url:"message/selectByPage"
        ,where:{
            "type":messagetype,
            "userCode":user.code
       }
        , page: {
            curr: 1 //重新从第 1 页开始
        }
        ,limit: 100
    }, 'data');
}


